GLS and Spatial Autocorrelation model fitting


In [1]:
# Load Biospytial modules and etc.
%matplotlib inline
import sys
sys.path.append('/apps')
sys.path.append('..')
sys.path.append('../../spystats')
import django
django.setup()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
## Use the ggplot style
plt.style.use('ggplot')

import tools

Read the data from Juan David

I'm going to include a link to the datasource


In [8]:
import os
input_path = "/RawDataCSV/DN_plot.data/"

In [19]:
filenames = os.listdir(input_path)
fullpath = map(lambda name : input_path+name,filenames)
pds = map(lambda file : pd.read_csv(file), fullpath)


/opt/conda/envs/biospytial/lib/python2.7/site-packages/IPython/core/interactiveshell.py:2821: DtypeWarning: Columns (23) have mixed types. Specify dtype option on import or set low_memory=False.
  if self.run_code(code, result):

In [23]:
sizes = map(lambda p : p.shape[0],pds)

In [24]:
sizes


Out[24]:
[100000,
 200000,
 300000,
 150000,
 300000,
 450000,
 200000,
 400000,
 600000,
 50000,
 100000,
 150000,
 100000,
 200000,
 300000,
 150000,
 300000,
 450000,
 200000,
 400000,
 600000,
 50000,
 100000,
 150000]

In [16]:
t1 = fullpath[0]
nd = pd.read_csv(t1)

In [ ]:
## We group by simulation index

In [29]:
groups = nd.groupby(['sim'])

In [46]:
##  PCOnvert to list to extract appropiate sub fitting, this will be expanded further

In [34]:
gg = list(groups)

In [47]:
t1 = gg[0][1]

In [49]:
## Convert to geopandas
gt1 =  tools.toGeoDataFrame(t1,xcoord_name='Lon',ycoord_name='Lat')



KeyErrorTraceback (most recent call last)
<ipython-input-49-ee9968b17f49> in <module>()
      1 ## Convert to geopandas
----> 2 gt1 =  tools.toGeoDataFrame(t1,xcoord_name='Lon',ycoord_name='Lat')

/apps/external_plugins/spystats/spystats/tools.pyc in toGeoDataFrame(pandas_dataframe, xcoord_name, ycoord_name, srs)
     57     """
     58     data = pandas_dataframe
---> 59     data['geometry'] = data.apply(lambda z : Point(z[xcoord_name], z[ycoord_name]), axis=1)
     60     #data['geometry'] = data.apply(lambda z : Point(z.LON, z.LAT), axis=1)
     61 

/opt/conda/envs/biospytial/lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4131                     if reduce is None:
   4132                         reduce = True
-> 4133                     return self._apply_standard(f, axis, reduce=reduce)
   4134             else:
   4135                 return self._apply_broadcast(f, axis)

/opt/conda/envs/biospytial/lib/python2.7/site-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
   4227             try:
   4228                 for i, v in enumerate(series_gen):
-> 4229                     results[i] = func(v)
   4230                     keys.append(v.name)
   4231             except Exception as e:

/apps/external_plugins/spystats/spystats/tools.pyc in <lambda>(z)
     57     """
     58     data = pandas_dataframe
---> 59     data['geometry'] = data.apply(lambda z : Point(z[xcoord_name], z[ycoord_name]), axis=1)
     60     #data['geometry'] = data.apply(lambda z : Point(z.LON, z.LAT), axis=1)
     61 

/opt/conda/envs/biospytial/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key)
    599         key = com._apply_if_callable(key, self)
    600         try:
--> 601             result = self.index.get_value(self, key)
    602 
    603             if not is_scalar(result):

/opt/conda/envs/biospytial/lib/python2.7/site-packages/pandas/indexes/base.pyc in get_value(self, series, key)
   2151                     raise InvalidIndexError(key)
   2152                 else:
-> 2153                     raise e1
   2154             except Exception:  # pragma: no cover
   2155                 raise e1

KeyError: ('Lon', u'occurred at index 0')

In [ ]: